OCFeatureFlagManager and OCFeatureFlagSyncManager
Overview
The OCFeatureFlagManager
and OCFeatureFlagSyncManager
are components designed to manage and synchronize feature flags in your application. Feature flags allow you to enable or disable specific functionality dynamically, making it easier to roll out new features, perform A/B testing, or toggle features based on user or environment conditions.
- OCFeatureFlagManager: Handles local storage, overrides, and persistence of feature flags.
- OCFeatureFlagSyncManager: Synchronizes feature flags with a remote API, ensuring your application stays up-to-date with the latest feature flag configurations.
OCFeatureFlagManager
Description
The OCFeatureFlagManager
is responsible for managing feature flags locally. It provides functionality to save, load, override, and reset feature flags, as well as retrieve their effective values.
Key Features
- Persistence: Save and load feature flags using local storage.
- Overrides: Temporarily override feature flag values.
- Effective Values: Automatically prioritize override values over default values.
- ETag Support: Track changes using ETags for efficient updates.
Protocol
The OCFeatureFlagManagerProtocol
defines the functionality of a feature flag manager:
public protocol OCFeatureFlagManagerProtocol {
func saveFeatureFlags(_ flags: [String: OCRemoteFeatureFlag], etag: String?)
func loadFeatureFlags()
func getEtag() -> String?
func getLastSaveTimestamp() -> TimeInterval?
func overrideFeatureFlag(_ featureFlag: String, withValue value: Bool)
func removeOverride(for featureFlag: String)
func resetOverrides()
func isEnabled(for featureFlag: String) -> Bool
func savedFeatureFlags() -> [String: OCFeatureFlag]
func isLocalStorageEmpty() -> Bool
}
Usage
Saving Feature Flags
await OCFeatureFlagManager.shared.saveFeatureFlags(flags, etag: "etag123")
Loading Feature Flags
await OCFeatureFlagManager.shared.loadFeatureFlags()
Overriding a Feature Flag
await OCFeatureFlagManager.shared.overrideFeatureFlag("featureFlagKey", withValue: true)
Removing an Override
await OCFeatureFlagManager.shared.removeOverride(for: "featureFlagKey")
Resetting Overrides
await OCFeatureFlagManager.shared.resetOverrides()
Retrieving a Feature Flag Value
let value = OCFeatureFlagManager.shared.isEnabled(for: "featureFlagKey")
Checking if Local Storage is Empty
let isEmpty = OCFeatureFlagManager.shared.isLocalStorageEmpty()
OCFeatureFlagSyncManager
Description
The OCFeatureFlagSyncManager
is responsible for synchronizing feature flags with a remote API. It ensures that your application has the latest feature flag configurations by fetching them from a server.
Key Features
- Remote Synchronization: Fetch and update feature flags from a remote API.
- ETag Support: Use ETags to optimize network requests and avoid unnecessary updates.
Usage
Synchronizing Feature Flags
let syncManager = OCFeatureFlagSyncManager(
featureFlagManager: OCFeatureFlagManager.shared,
networkManager: networkManager,
featureFlagRequest: request
)
let success = await syncManager.synchronize()
Key Classes and Structs
OCFeatureFlagManager
Manages feature flags locally, including saving, loading, overriding, and resetting.
OCFeatureFlagSyncManager
Synchronizes feature flags with a remote API.
OCFeatureFlag
Represents a feature flag with its default value and optional override value.
public struct OCFeatureFlag: Codable {
public var isEnabled: Bool
public var displayName: String
public var overrideValue: Bool?
public var effectiveValue: Bool {
overrideValue ?? isEnabled
}
public init(remoteFlag: OCRemoteFeatureFlag) {
self.isEnabled = remoteFlag.enabled
self.displayName = remoteFlag.displayName
}
}
OCRemoteFeatureFlag
Represents a feature flag fetched from a remote API.
public struct OCRemoteFeatureFlag: Codable {
public var enabled: Bool
public var displayName: String
}
Example Workflow
-
Save Feature Flags Locally:
OCFeatureFlagManager.shared.saveFeatureFlags(flags, etag: "etag123")
-
Synchronize with Remote API:
let syncManager = OCFeatureFlagSyncManager(
featureFlagManager: OCFeatureFlagManager.shared,
networkManager: networkManager,
featureFlagRequest: request
)
let success = await syncManager.synchronize() -
Override a Feature Flag:
OCFeatureFlagManager.shared.overrideFeatureFlag("featureFlagKey", withValue: true)
-
Retrieve Effective Value:
let value = OCFeatureFlagManager.shared.isEnabled(for: "featureFlagKey")
-
Reset Overrides:
OCFeatureFlagManager.shared.resetOverrides()
Best Practices
- Keep Feature Flags Short-Lived: Remove unused feature flags to avoid clutter.
- Use Meaningful Keys: Use descriptive keys for feature flags to improve readability.
- Test Feature Flags: Ensure feature flags are tested in both enabled and disabled states.
- Secure Remote API: Protect the API used for synchronization to prevent unauthorized access.
Conclusion
The OCFeatureFlagManager
and OCFeatureFlagSyncManager
provide a robust solution for managing and synchronizing feature flags in your application. By leveraging these components, you can dynamically control application behavior, roll out features safely, and optimize user experiences.